package game.module.dabt.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import game.module.dabt.bean.DabtBean;

public class DabtCache {

	private static Logger logger = LoggerFactory.getLogger(DabtCache.class);

	static class SingletonHolder {
		static DabtCache instance = new DabtCache();
	}

	public static DabtCache getInstance() {
		return SingletonHolder.instance;
	}

	public Map<Integer, List<DabtBean>> dabtsCacheall = new ConcurrentHashMap<Integer, List<DabtBean>>();

	public void LoadFromDb(int playerId) {
		if (dabtsCacheall.containsKey(playerId)) {
			return;
		}
		List<DabtBean> dabts = DabtdbDao.getInstance().getDabtBeanById(playerId);
		if (dabts != null) {
			dabtsCacheall.put(playerId, dabts);
		}
		logger.info("playerid={},load dabt", playerId);
	}

	public void remove(int playerId) {
		dabtsCacheall.remove(playerId);
	}

	public List<DabtBean> getDabt(int playerId) {
		if (dabtsCacheall.containsKey(playerId)) {
			return dabtsCacheall.get(playerId);
		} else {
			return null;
		}
	}

	public void addDabtBean(DabtBean bean) {
		if (bean == null) {
			logger.error(" add dabt Bean is null");
			return;
		}

		if (!dabtsCacheall.containsKey(bean.getPlayerid())) {
			dabtsCacheall.put(bean.getPlayerid(), new ArrayList<>());
		}
		dabtsCacheall.get(bean.getPlayerid()).add(bean);
	}
}
